package translationTable;

import java.io.FileReader;
import java.net.URL;
import java.util.Enumeration;
import nanoxml.XMLElement;

/**
 *
 * @author karimnaaji
 */
public class XMLFileStream {
    private String path;
    private XMLElement root;

    public XMLFileStream(URL url) {
        path = url.getPath();
        root = new XMLElement();
        initRoot();
    }
    
    public XMLFileStream(String xmlPath) {
        path = xmlPath;
        root = new XMLElement();
        initRoot();
    }
    
    private void initRoot() {
        try {            
            FileReader reader = new FileReader(path);
            root.parseFromReader(reader);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println("Error trying to load XML file, please check your xml file name.");
        }
    }

    private void DisplayTreeNode(XMLElement node) {
        String display = "<" + node.getName();
        if(node.getAttribute("name") != null) display+= " name="+node.getAttribute("name");
        if(node.getAttribute("class") != null) display += " class="+node.getAttribute("class");
        System.out.println(display+">");
        
        if(!node.getContent().equals("")) {
            System.out.println("content: " + node.getContent());
        }
        Enumeration enumeration = node.enumerateChildren();
        while (enumeration.hasMoreElements()) {
            XMLElement nextNode = (XMLElement) (enumeration.nextElement()); 
            DisplayTreeNode(nextNode);
        }
    }
    
    public void DisplayTreeNode() {
        DisplayTreeNode(root);
    }
    
    public XMLElement getRoot() {
        return root;
    }
}
